package server;

import codec.FileCodecFactory;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.net.InetSocketAddress;

import static org.apache.mina.core.session.IdleStatus.BOTH_IDLE;

/**
 * Created by ryan on 15-8-25.
 */
public class FileServer {
    public static int BUFFER_SIZE;
    public static String fileName = "test.txt";
    public static void open(String[] args) {
        FileServer server = new FileServer();
        System.out.println("Server start....");
        int port = Integer.parseInt(args[0]);
        BUFFER_SIZE = Integer.parseInt(args[1]);
        System.out.println("port\t: " + port);
        try {
            IoAcceptor acceptor = new NioSocketAcceptor();
            acceptor.getSessionConfig().setReadBufferSize(1024 * 2);
            acceptor.getSessionConfig().setIdleTime(BOTH_IDLE, 10);
            acceptor.getFilterChain().addLast("codec",
                    new ProtocolCodecFilter(new FileCodecFactory(false)));
            acceptor.setHandler(new FileServerIoHandler());
            acceptor.bind(new InetSocketAddress(port));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
